/*
 * Created: Author: Ivan Lampert / Date: Jan 26, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-05-13 13:23:31 +0000 (Wed, 13 May 2015) $
 */
package com.cpm.entities.converters;

import com.cpm.wrapper.GenericType;
import com.cpm.wrapper.SolidObjects;
import com.cpm.wrapper.exception.NotFoundException;

public final class TypeConverter {
    
    private TypeConverter() {}

    public static final <T> T getId(GenericType<T> genericType) {
	return SolidObjects.notNull(genericType).getId();
    }

    public static final <T> T convertTo(Class<? extends GenericType> resultType, Object key) {
	SolidObjects.notNullValues(resultType, key);

        final GenericType[] values = resultType.getEnumConstants();

        for ( GenericType type : values ) {
            if ( String.valueOf( type.getId() ).equalsIgnoreCase( String.valueOf(key) ) ) {
                return (T) type;
            }
        }

        throw new NotFoundException("O Type para o " + String.valueOf(key) + " na classe " + resultType.getSimpleName() + " não foi encontrado.");
    }
}
